package com.lv.LvRefundRatioInfo.web;
import com.aqgj.User.bean.User;
import com.aqgj.util.JsonWrapper;
import com.lv.LvRefundRatio.bean.LvRefundRatio;
import com.lv.LvRefundRatio.dao.LvRefundRatioDao;
import com.lv.LvRefundRatio.service.LvRefundRatioManager;
import com.lv.LvRefundRatioInfo.bean.LvRefundRatioInfo;
import com.lv.LvRefundRatioInfo.dao.LvRefundRatioInfoDao;
import com.lv.LvRefundRatioInfo.service.LvRefundRatioInfoManager;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@Controller
@RequestMapping("lvRefundRatioInfo")
public class LvRefundRatioInfoAction {
    @Autowired
    private LvRefundRatioInfoManager lvRefundRatioInfoManager;
    @Autowired
    private LvRefundRatioManager lvRefundRatioManager;
    @Autowired
    private LvRefundRatioDao lvRefundRatioDao;
    @Autowired
    private LvRefundRatioInfoDao lvRefundRatioInfoDao;
    @Autowired
    private ZtcTransportManager ztcTransportManager;


    /**
     * 保存扣费比例设置.
     * @param lvRefundRatio     扣费比例详情规则
     * @param lvRefundRatioInfo 扣费比例设置信息
     * @return
     */
    @ResponseBody
    @RequestMapping("saveRefundRatio")
    @Transactional
    public HashMap<String, Object> saveRefundRatio(HttpSession session, String lvRefundRatio, LvRefundRatioInfo lvRefundRatioInfo) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null || "".equals(user)) {
            return JsonWrapper.failureWrapperMsg("用户未登录!");
        }
        ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
        int transportType = transport.getType();
        if (transportType == 0 || transportType == 1 || transportType == 2) {
            return JsonWrapper.failureWrapperMsg("非旅游公司不可使用此功能!");
        }
        if (transport.getStatus() == 4) {
            return JsonWrapper.failureWrapperMsg("该用户公司已被冻结，不可使用此功能!");
        }
        if(lvRefundRatioInfo.getId()!=null||!"".equals(lvRefundRatioInfo.getId())){
            LvRefundRatioInfo lvRefundRatioInfo1= lvRefundRatioInfoManager.get(lvRefundRatioInfo.getId());
            lvRefundRatioInfo.setFkAddUserName(lvRefundRatioInfo1.getFkAddUserName());
            lvRefundRatioInfo.setFkAddUserId(lvRefundRatioInfo1.getFkAddUserId());
            lvRefundRatioInfo.setAddTime(lvRefundRatioInfo1.getAddTime());
            lvRefundRatioInfo.setModifyTime(new Date());
            lvRefundRatioInfo.setStatus(lvRefundRatioInfo1.getStatus());
            lvRefundRatioInfo.setFkTransportId(lvRefundRatioInfo1.getFkTransportId());
            List<LvRefundRatio> lvRefundRatioList=lvRefundRatioDao.findBy("fkLvRefundRatioInfoId",lvRefundRatioInfo.getId());
            if(lvRefundRatioList.size()>0){
                for (LvRefundRatio z:lvRefundRatioList) {
                    lvRefundRatioManager.removeById(z.getId());
                }
            }
        }
        return lvRefundRatioInfoManager.saveInfo(lvRefundRatioInfo, lvRefundRatio);
    }

    /**
     * 获取扣费比例设置详情信息.
     * @return
     */
    @ResponseBody
    @RequestMapping("getLvRefundRatioInfo")
    public HashMap<String,Object> getZtcRefundRatioInfo(HttpSession session){
        User user = (User) session.getAttribute("userInfo");
        List<LvRefundRatioInfo> list = lvRefundRatioInfoDao.findBy("fkTransportId", user.getTransportId());
        HashMap<String, Object> map = new HashMap<>();
        if(list.size()>0){
            List<LvRefundRatio> list1 = lvRefundRatioDao.findBy("fkLvRefundRatioInfoId", list.get(0).getId());
            if (list1.size() > 0) {
                map.put("id", list.get(0).getId());
                map.put("ratioName", list.get(0).getRatioName());
                map.put("remark", list.get(0).getRemark());
                map.put("ratioInfo", list1);
            }
        }else{
            map.put("id", "");
            map.put("ratioName", "");
            map.put("remark", "");
            map.put("ratioInfo", "");
        }
        return JsonWrapper.successWrapper(map);
    }
}